<template>
  <div class="result-container">
    <!-- 导航栏 -->
    <van-nav-bar
      :title="`${$route.query.q} 的搜索结果`"
      left-arrow
      fixed
      @click-left="$router.back()"
    />
    <!-- /导航栏 -->

    <!-- 文章列表 -->
    <van-list
      class="article-list"
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="(item, index) in list"
        :key="index"
        :title="item.title"
      />
    </van-list>
    <!-- /文章列表 -->
  </div>
</template>

<script>
import { searchArticles } from '../../api/search'
export default {
  name: 'result',
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1,
      per_page: 10
    }
  },
  methods: {
    async onLoad () {
      const { q } = this.$route.query
      const res = await searchArticles({
        q,
        page: this.page,
        per_page: this.per_page
      })
      // console.log('res', res)
      this.list.push(...res.data.data.results)
      // 加载状态结束
      this.loading = false
      this.page++
      // 数据全部加载完成
      if (this.page >= 4) {
        this.finished = true
      }
    }
  }
}
</script>

<style lang="less">
.result-container {
  .article-list {
    margin-top: 39px;
  }
  .van-nav-bar {
    background-color: #3296fa;
    .van-nav-bar__title {
      color: white;
    }
    .van-icon {
      color: white;
    }
  }
}
</style>
